package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

type ReaderPhotoRequest struct {
	Code             util.Field
	TransactionDate  util.Field
	InstitutionId    util.FieldPrefix
	PatronIdentifier util.FieldPrefix
	PhotoLE          util.FieldPrefix
	PhotoDA          util.FieldPrefix
	Operator         util.FieldPrefix
}

func ReaderPhoto(str string) (*ReaderPhotoRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	util.Code.Value = "77"

	var query = ReaderPhotoRequest{
		Code:             util.Code,
		TransactionDate:  util.TransactionDate,
		InstitutionId:    util.InstitutionId,
		PatronIdentifier: util.PatronIdentifier,
		PhotoLE:          util.PhotoLE,
		PhotoDA:          util.PhotoDA,
		Operator:         util.Operator,
	}

	length := query.Code.Length + query.TransactionDate.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.TransactionDate.GetValue(strTemp)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.PatronIdentifier.GetValue(strTemp)
	strTemp = query.PhotoLE.GetValue(strTemp)
	strTemp = query.PhotoDA.GetValue(strTemp)
	_ = query.Operator.GetValue(strTemp)

	return &query, nil
}

type ReaderPhotoResponse struct {
	Code            util.Field
	TransactionDate util.Field
	OK              util.FieldPrefix
	ScreenMessage   util.FieldPrefix
}

func (q ReaderPhotoResponse) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitReaderPhotoReq() *ReaderPhotoResponse {
	// 8220240623    182709AO|AA371521199005150927|JK ]] ]] ]] |OK1|XD2051-11-09|AF办理新证成功!|AG办理新证成功!
	util.Code.Value = "78"
	util.TransactionDate.Value = util.GetCurrentTimeInFormat()
	util.OK.Value = "1"
	util.ScreenMessage.Value = "更新成功!"
	var queryResponse = ReaderPhotoResponse{
		Code:            util.Code,
		TransactionDate: util.TransactionDate,
		OK:              util.OK,
		ScreenMessage:   util.ScreenMessage,
	}

	return &queryResponse
}
